package com.cgwx.tcservice.netty;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgwx.tcservice.entity.bean.TcCommandConfigMap;
import com.cgwx.tcservice.entity.bean.TcFrameConfigIdToIdentified;
import com.cgwx.tcservice.entity.bean.TcParameterConfigMap;
import com.cgwx.tcservice.entity.tc.TcCommandConfig;
import com.cgwx.tcservice.entity.tc.TcFrameConfig;
import com.cgwx.tcservice.entity.tc.TcParameterConfig;
import com.cgwx.tcservice.service.TcCommandConfigService;
import com.cgwx.tcservice.service.TcFrameConfigService;
import com.cgwx.tcservice.service.TcParameterConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GaoWei
 * @Date 2023/8/30 - 11:01
 */
@Component
public class UdpSendInitApplication implements ApplicationRunner {

    @Autowired
    private TcFrameConfigService tcFrameConfigService;
    @Autowired
    private TcCommandConfigService tcCommandConfigService;
    @Autowired
    private TcParameterConfigService tcParameterConfigService;

    @Override
    public void run(ApplicationArguments args){
        tcConfigInit();
    }

    public void tcConfigInit(){
//        遍历所有遥控帧配置，得到id为key identifiers为value，存储
        List<TcFrameConfig> tcFrameConfigList = tcFrameConfigService.list();
        for (TcFrameConfig tcFrameConfig : tcFrameConfigList) {
            TcFrameConfigIdToIdentified.TC_FRAME_ID_IDENTIFIED.put(tcFrameConfig.getId(),tcFrameConfig.getTcFrameIdentifiers());
        }

//        遍历所有遥控指令配置，得到id为key tcCommandCode为value，存储
        List<TcCommandConfig> tcCommandConfigList = tcCommandConfigService.list();
        for (TcCommandConfig tcCommandConfig : tcCommandConfigList) {
            TcCommandConfigMap.TC_COMMAND_ID_TCCOMMANDCONFIG.put(tcCommandConfig.getId(),tcCommandConfig);
//            根据遥控指令代号遍历所有遥控参数配置，得到遥控指令代号tc_code为key ArrayList<TcParameterConfig>为value，存储
            QueryWrapper<TcParameterConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tc_command_code", tcCommandConfig.getTcCommandCode());
            List<TcParameterConfig> tcParameterConfigListByTcCode = tcParameterConfigService.list(queryWrapper);
            ArrayList<TcParameterConfig> tcParameterConfigArrayList = new ArrayList<>(tcParameterConfigListByTcCode);
            TcParameterConfigMap.TC_TCCODE_TO_PARAMETER_CONFIG_LIST.put(tcCommandConfig.getTcCommandCode(),tcParameterConfigArrayList);
        }
    }
}
